//
//  UserManager.m
//  QuickCure1
//
//  Created by apple on 2016/11/3.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "UserManager.h"

@implementation UserManager{
    NSMutableArray<DoctorApplyModel *> *_famousDoctorModelList;
}

#pragma mark
#pragma mark - 单例创建
static id _instance;
+(instancetype)sharedManager{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}


//懒加载famousDoctorModelList
-(NSMutableArray<DoctorApplyModel *> *)famousDoctorModelList{
    if(!_famousDoctorModelList){
        _famousDoctorModelList = [NSMutableArray array];
    }
    return _famousDoctorModelList;
}

//病例历史记录管理数组
-(NSMutableArray<PresonMondel *> *)presonBingLIMDList
{
    if (!_presonBingLIMDList) {
        _presonBingLIMDList = [[NSMutableArray alloc]init];
    }
    return _presonBingLIMDList;
}


//注销用户
-(void)cancellationUserInfo{
    
    // 创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //删除图片
    [self.presonBingLIMDList enumerateObjectsUsingBlock:^(PresonMondel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *photoPath = model.imgPath;
        [manager removeItemAtPath:photoPath error:nil];
    }];
    
    
    // 删除病例
    NSString *casesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString *casesFilePath = [casesPath stringByAppendingPathComponent:self.userInfo.card_number.description];
    [manager removeItemAtPath:casesFilePath error:nil];
    
    
    
    // 删除
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userInfo.plist"];
    [manager removeItemAtPath:filePath error:nil];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:kIsUserLoginKey];
    [[NSUserDefaults standardUserDefaults]synchronize];
}



-(void)setUserInfo:(Users *)userInfo{
    
    _userInfo = userInfo;
    [self saveUserInfo];
    [self loadCasesFromCache];
}



-(void)setFamousDoctorModelList:(NSMutableArray<DoctorApplyModel *> *)famousDoctorModelList{
    
    _famousDoctorModelList = famousDoctorModelList;
    [self saveFamousDoctorModelList];
    
}




//存储当前User信息进沙盒
-(void)saveUserInfo{
    
    NSString *Path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [Path stringByAppendingPathComponent:@"userInfo.plist"];
    
    [NSKeyedArchiver archiveRootObject:self.userInfo toFile:filePath];
    
}




//从沙盒去取出User信息
-(void)getUserInfoFromCache{
    
    NSString *Path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [Path stringByAppendingPathComponent:@"userInfo.plist"];
    
    self.userInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    //取出名名医申请数组
    [self loadFamousDoctorModelList];
    //取出病例数组
    [self loadCasesFromCache];
}


//存储病例到沙盒
-(void)saveCasesToCache{
    
    //存病例图片到cache
    [self.presonBingLIMDList enumerateObjectsUsingBlock:^(PresonMondel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImage* photo = model.casesPhoto;
        NSData *photoData = UIImagePNGRepresentation(photo);
        
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        NSString *imgName = [self getCurrentTimeStr];
        NSString *filePath = [cachePath stringByAppendingPathComponent:imgName];
        model.imgPath = imgName;
        
        [photoData writeToFile:filePath atomically:YES];
        NSLog(@"save   %@",filePath);
    }];
    
    //把数组归档
    NSString *Path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",self.userInfo.card_number.description]];
    [NSKeyedArchiver archiveRootObject:self.presonBingLIMDList toFile:filePath];
    
}


//存储名医申请进沙盒
-(void)saveFamousDoctorModelList{
    
    //把数组归档
    NSString *Path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [Path stringByAppendingPathComponent:self.userInfo.user_id.description];
    [NSKeyedArchiver archiveRootObject:self.famousDoctorModelList toFile:filePath];
    
    NSLog(@"%@",filePath);
    
}

//取出名医申请
-(void)loadFamousDoctorModelList{
    
    NSString *Path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [Path stringByAppendingPathComponent:self.userInfo.user_id.description];
    
    self.famousDoctorModelList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    
}




//得到当前时间字符串作为图片文件名
-(NSString*)getCurrentTimeStr{
    
    NSDate *date = [NSDate date];
    NSDateFormatter *fomatter = [[NSDateFormatter alloc]init];
    fomatter.dateFormat = @"yyyy_MM_dd_HH_mm_SS";
    NSString *dateStr = [fomatter stringFromDate:date];
    return dateStr;
}


//从沙盒取出病例数组
-(void)loadCasesFromCache{
    
    //先得到数组
    NSString *Path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",self.userInfo.card_number.description]];
    
    self.presonBingLIMDList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    //循环得到图片
    [self.presonBingLIMDList enumerateObjectsUsingBlock:^(PresonMondel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        NSString *imgName = model.imgPath;
        NSString *filePath = [cachePath stringByAppendingPathComponent:imgName];
        UIImage *photo = [UIImage imageWithContentsOfFile:filePath];
        model.casesPhoto = photo;
        NSLog(@"load   %@",filePath);
    }];
    
}




@end
